# api/app/models.py (v2)
from sqlalchemy import create_engine, Column, String, DateTime, Float, Enum as SQLEnum
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime
import enum

DATABASE_URL = "postgresql://agriuser:agripassword@db/agridb"

engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

class SubmissionStatus(str, enum.Enum):
    PENDING = "PENDING"
    RUNNING = "RUNNING"
    COMPLETED = "COMPLETED"
    FAILED = "FAILED"

class Submission(Base):
    __tablename__ = "submissions"

    id = Column(String, primary_key=True, index=True)
    team_id = Column(String, index=True)
    status = Column(SQLEnum(SubmissionStatus), default=SubmissionStatus.PENDING)
    submitted_at = Column(DateTime, default=datetime.datetime.utcnow)
    started_at = Column(DateTime, nullable=True)
    completed_at = Column(DateTime, nullable=True)
    zip_file_path = Column(String)

    total_score = Column(Float, nullable=True)
    single_choice_score = Column(Float, nullable=True)
    multi_choice_score = Column(Float, nullable=True)
    short_answer_score = Column(Float, nullable=True)
    multimodal_score = Column(Float, nullable=True)
    error_log = Column(String, nullable=True) # 用于记录失败原因

def create_tables():
    Base.metadata.create_all(bind=engine)